package com.deke.system.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.deke.entity.AclPermission;
import com.deke.security.annotation.RequiresPermission;
import com.deke.system.service.AclPermissionService;
import com.deke.system.vo.FenPermissionVo;
import com.deke.system.vo.PermissionVo;
import com.deke.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：Username 刘亦辰（59372312@qq.com）
 * @date ：Created in 2022/12/1 19:24
 * @description：
 * @modified By：
 * @version:
 **/
@RestController
@RequestMapping("/system/permission/")
@Api(tags = "权限接口类")
public class AclPermissionController {
    @Autowired
    private AclPermissionService permissionService;

    @GetMapping("/queryPermissionByUserid/{userid}")
    @ApiOperation(value = "根据用户id查询用户权限")
    @ApiImplicitParam(value = "用户ID",name = "userid")
    public Result<AclPermission> queryPermissionByUserid(@PathVariable String userid){
        List<AclPermission> aclPermissions = permissionService.queryPermissionByUserid(userid);
        if (ObjectUtil.isNotNull(aclPermissions) && aclPermissions.size()>0){
            Result<AclPermission> result = new Result(2000,"查询成功",aclPermissions);
            return result;
        }else {
            Result<AclPermission> result = new Result(2000, "该用户没有权限", null);
            return result;
        }
    }

    @GetMapping("/getAllPermission")
    @ApiOperation(value = "用过用户名查询用户的权限")
    public Result<List<AclPermission>> getAllPermission(){
        return permissionService.queryPermissionByUsername();
    }
    
    /**
     * @create by: Username 刘亦辰 
     * @description: 角色管理 根据角色id  查权限 
     * @create time: 2022/12/6 16:53
     * @param   roleId
     * @return :  Result
     **/
    @GetMapping("/findPermissionByRoleId/{roleId}")
    @ApiOperation(value = "通过角色的id查询角色的权限")
    @ApiImplicitParam(value = "角色ID",name = "roleId")
    public Result<Map<String,Object>> findPermissionByRoleId(@PathVariable String roleId){
        return  permissionService.findPermissionByRoleId(roleId);
    }
    
    /**
     * @create by: Username 刘亦辰 
     * @description:  角色管理 分配权限
     * @create time: 2022/12/6 18:11
     * @param null
     * @return :  
     **/
    @PostMapping("confirmFenPermission")
    @ApiOperation(value = "给角色分配权限")
    @ApiImplicitParam(value = "前端实体类",name = "fenPermissionVo")
    public Result confirmFenPermission(@RequestBody FenPermissionVo fenPermissionVo){
        return  permissionService.confirmFenPermission(fenPermissionVo);
    }
    
    
    /**
     * @create by: Username 刘亦辰 
     * @description: 菜单管理   模糊查询 
     * @create time: 2022/12/6 18:37
     * @param null
     * @return :  
     **/
    @PostMapping("/findAllByPage/{currentPage}/{pageSize}")
    @ApiOperation(value = "菜单管理的模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页码数",name = "currentPage"),
            @ApiImplicitParam(value = "每页显示行数",name = "pageSize"),
            @ApiImplicitParam(value = "模糊查询实体类",name = "permissionVo")
    })
    public Result findAllByPage(@PathVariable Integer currentPage, @PathVariable Integer pageSize,@RequestBody PermissionVo permissionVo){
        return permissionService.queryByConditionPage(currentPage, pageSize, permissionVo);
    }
    


    /**
     * @create by: Username 刘亦辰 
     * @description: 菜单管理 权限添加方法 
     * @create time: 2022/12/6 21:16
     * @param null
     * @return :  
     **/
    @RequiresPermission("permission:add")
    @PutMapping("insert")
    @ApiOperation(value = "添加菜单,按钮")
    @ApiImplicitParam(value = "前端实体类",name = "permissionVo")
    public Result insert(@RequestBody PermissionVo permissionVo){
        return permissionService.insert(permissionVo);
    }

    /**
     * @create by: Username 刘亦辰 
     * @description: 菜单管理  修改信息
     * @create time: 2022/12/6 21:37
     * @param null
     * @return :  
     **/
    @RequiresPermission("permission:update")
    @PostMapping("update")
    @ApiOperation(value = "更改菜单,按钮")
    @ApiImplicitParam(value = "前端实体类",name = "permissionVo")
    public Result update(@RequestBody PermissionVo permissionVo){
        AclPermission aclPermission = new AclPermission();
        aclPermission.setGmtModified(permissionVo.getGmtModified());
        aclPermission.setIcon(permissionVo.getIcon());
        aclPermission.setId(permissionVo.getId());
        aclPermission.setName(permissionVo.getName());
        aclPermission.setPath(permissionVo.getPath());
        aclPermission.setPermissionValue(permissionVo.getPermissionValue());
        aclPermission.setPid(permissionVo.getPid());
        if (permissionVo.getType().equals("1")){
            aclPermission.setType(1);
        }else {
            aclPermission.setType(2);
        }
        return permissionService.update(aclPermission);
    }

    /**
     * @create by: Username 刘亦辰 
     * @description: 菜单管理 删除 
     * @create time: 2023/2/12 12:05
     * @param null
     * @return :  
     **/
    @RequiresPermission("permission:remove")
    @PostMapping("deleteById")
    @ApiOperation(value = "删除目录")
    @ApiImplicitParam(value = "前端实体类",name = "permissionVo")
    public Result deleteById(@RequestBody PermissionVo permissionVo){
       // List<ArrayList> idList = JSON.parseArray(JSON.parseObject(ids).getString("ids"), ArrayList.class);
        return permissionService.deleteById(permissionVo.getPermissionIds());
    }


    
}